<template>
  <div>
    <el-table
      :data="goodslist"
      style="width: 100%;"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children'}"
    >
      <el-table-column prop="id" label="商品编号" width="160"></el-table-column>
      <el-table-column prop="goodsname" label="商品名称" width="160"></el-table-column>
      <el-table-column prop="price" label="商品价格"></el-table-column>
      <el-table-column prop="market_price" label="市场价格"></el-table-column>
      <el-table-column prop="img" label="图片" width='200px'>
        <template slot-scope="item">
          <div>
            <img v-if="item.row.img" class="imgUrl" :src="$imgUrl+item.row.img" alt />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否新品">
        <template slot-scope="item">
          <div>
            <el-tag v-if="item.row.status==1" type="success">是</el-tag>
            <el-tag v-else type="danger">否</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否热卖">
        <template slot-scope="item">
          <div>
            <el-tag v-if="item.row.status==1" type="success">是</el-tag>
            <el-tag v-else type="danger">否</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="item">
          <div>
            <el-tag v-if="item.row.status==1" type="success">启动</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="item">
          <div>
            <el-button type="danger" icon="el-icon-delete" @click="del(item.row.id)" circle></el-button>
            <el-button type="primary" icon="el-icon-edit" @click="edit(item.row.id)" circle></el-button>
            <el-button icon="el-icon-search" circle></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { goodsList, goodsDelete } from "../../util/request/api";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      goodslist: "goods/getGoodsList",
      specslist:'specs/getSpecsList'
    })
  },
  mounted() {
    //组件一加载就获取菜单列表
    this.getGoodslist();
    this.getSpecsList()
  },
  methods: {
    ...mapActions({
      getGoodslist: "goods/getGoodsAction",
      getSpecsList:'specs/getSpecsAction'
    }),
    cancel(e) {
      this.info.isshow = e;
    },
    edit(id) {
      this.$emit("edit", id);
    },
    del(id) {
      this.$confirm("你确定要删除当前这条数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          //调用删除接口
          goodsDelete({ id }).then(res => {
            this.getGoodslist();

            if (res.code == 200) {
              //删除成功，要重新调用接口列表
              goodsList().then(res => {
                console.log(res, "菜单列表");
                if (res.code == 200) {
                }
              });
              this.$message.success(res.msg);
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped>
.el-menu {
  margin: -20px;
  margin-bottom: 20px;
}
.imgUrl{
  width: 190px;
  height: 150px;
}
</style>